/**
 * Created by liangruizhe on 2019/01/14
 */
//==============时间===============
//获取当前时间
function GetCurrentTime(){
    var today = new Date();
    var year = today.getFullYear();
    var month = today.getMonth() + 1;
    var day = today.getDate()>9 ? today.getDate() : '0'+ today.getDate();
    var hours = today.getHours()>9 ? today.getHours() : '0'+ today.getHours();
    var minutes = today.getMinutes()>9 ? today.getMinutes() : '0'+ today.getMinutes();
    var seconds = today.getSeconds()>9 ? today.getSeconds() : '0'+ today.getSeconds();
    return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;
}
//获取当天零点时间
function GetCurrentDayTime(){
    var today = new Date();
    var year = today.getFullYear();
    var month = today.getMonth() + 1;
    var day = today.getDate()>9 ? today.getDate() : '0'+ today.getDate();
    return year + '-' + month + '-' + day + ' 00:01:01';
}
//UtcToDateTime
function UtcToDateTime(time){
    var date = new Date(time);
    var year=date.getFullYear();
    var month=date.getMonth()+1;
    var day=date.getDate();
    var hour=date.getHours();
    var minute=date.getMinutes();
    var second=date.getSeconds();
    return year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second;
}
//UtcToDateTime
function convertDateToString(date){
    var year=date.getFullYear();
    var month=date.getMonth()+1;
    var day=date.getDate();
    var hour=date.getHours();
    var minute=date.getMinutes();
    return year+"年"+month+"月"+day+"日 "+hour+":"+minute;
}
// yyyy-MM-dd
function convertDateFromString(dateString) {
    if (dateString) {
        var date = new Date(dateString.replace(/-/,"/"));
        return date;
    }
}
// yyyy-MM-dd hh:mm:ss
function convertDateTimeFromString(dateString) {
    if (dateString) {
        var arr1 = dateString.split(" ");
        var sdate = arr1[0].split('-');
        var stime = arr1[1].split(':');
        var date = new Date(sdate[0], sdate[1]-1, sdate[2],stime[0], stime[1], stime.length > 2 ? stime[2] : '00');
        return date;
    }
}
function padLeft (oriStr,len,alexin) {
    let str = "";
    let strlen = oriStr.length;
    if(strlen < len){
        for(let i=0;i<len-strlen;i++){
            str = str+alexin;
        }
    }
    str = str + oriStr;
    return str;
}
//==============空间计算===============
// 两点计算距离
function _calDistance(point1, point2) {
    var line = new esri.geometry.Polyline(map.spatialReference);
    line.addPath([point1, point2]);
    return Number(getLength([point1, point2]));
}
// 计算点串距离
function getLength (arrPnts, isJwd) {
    var dist = 0;
    var length = 0;
    var offset = 103133.845;
    var fntPnt = null;
    var lastPnt = null;
    var res = 0;
    if (isJwd == undefined) isJwd = true;
    if (arrPnts.length < 2)
    {
        return 0;
    }
    for (var i = 0;i < arrPnts.length - 1;i++)
    {
        fntPnt = arrPnts[i];
        lastPnt = arrPnts[i + 1];
        if (lastPnt) {

            var xD = Math.abs(fntPnt.x - lastPnt.x);
            var yD = Math.abs(fntPnt.y - lastPnt.y);

            if (isJwd) {
                if (Math.abs(fntPnt.x) > 180) {
                    xD = xD / 3600;
                    yD = yD / 3600;
                }
                dist = (Math.sqrt(Math.pow(xD, 2) + Math.pow(yD, 2))) * offset;
            }
            else {
                dist = (Math.sqrt(Math.pow(xD, 2) + Math.pow(yD, 2)));
            }

            length += dist;

        }
    }
    return length;
}
// 计算点到线的距离
function getDistance(point,line){


}
// 计算点到线段的距离
function getDistance(point,lineP1,lineP2){
    var a = Number((lineP1.y - lineP2.y)/(lineP1.x - lineP2.x));
    var b = -1;
    var c = Number(lineP2.y - a * lineP2.x);
    var td = Math.abs( a * point.x + b * point.y + c )/Math.sqrt( a * a + b * b );
    return td;
}


//==============ajax请求===============
function GetDataByUrl(pUrl,pData,pBackFun){
    $.ajax({
        type: 'GET',
        url: pUrl,
        data: pData,
        timeout: 600000,
        success: function (datas, nnn) {
            pBackFun(datas);
        },
        error: function (e, nnnn) {
            console.error('数据获取失败');
            $('.warning-content span').html('数据获取失败');
            $('.warning').css('display', 'block');
            return;
        }
    })
}

function GetDataAsynByUrl(pUrl,pData,pBackFun){
    $.ajax({
        type: 'GET',
        url: pUrl,
        data: pData,
        timeout: 600000,
        async:true,
        success: function (datas, nnn) {
            if (datas.code == "200" && datas.body != null) {
                pBackFun(datas.body);
            }else{
                console.error(datas.code + ":" + datas.msg);
                $('.warning-content span').html(datas.code + ":" + datas.msg);
                $('.warning').css('display', 'block');
                return;
            }
        },
        error: function (e, nnnn) {
            console.error('数据获取失败');
            $('.warning-content span').html('数据获取失败');
            $('.warning').css('display', 'block');
            return;
        }
    })
}
function PostDataAsynByUrl(pUrl,pData,pBackFun){
    $.ajax({
        type: 'POST',
        url: pUrl,
        data: pData,
        timeout: 600000,
        contentType : "application/x-www-form-urlencoded;charset=utf-8",
        async:false,
        dataType:'json',
        success: function (datas, nnn) {
            if (datas.code == "200") {
                pBackFun(datas);
            }else{
                console.error(datas.code + ":" + datas.msg);
                $('.warning-content span').html(datas.code + ":" + datas.msg);
                $('.warning').css('display', 'block');
                return;
            }
        },
        error: function (e, nnnn) {
            console.error('数据获取失败');
            $('.warning-content span').html('数据获取失败');
            $('.warning').css('display', 'block');
            return;
        }
    })
}
function GetJsonDataAsynByUrl(pUrl,pData,pBackFun){
    $.ajax({
        type: 'GET',
        url: pUrl,
        dataType : "json",
        data:  pData,
        timeout: 600000,
        async:true,
        success: function (datas, nnn) {
            if (datas.code == "200") {
                pBackFun(datas);
            }else{
                console.error(datas.code + ":" + datas.msg);
                $('.warning-content span').html(datas.code + ":" + datas.msg);
                $('.warning').css('display', 'block');
                return;
            }
        },
        error: function (e, nnnn) {
            console.error('数据获取失败');
            $('.warning-content span').html('数据获取失败');
            $('.warning').css('display', 'block');
            return;
        }
    })
}
function GetJsonDataByUrl(pUrl,pData,pBackFun){
    $.ajax({
        type: 'GET',
        url: pUrl,
        dataType : "json",
        data:  pData,
        timeout: 600000,
        async:false,
        success: function (datas, nnn) {
            pBackFun(datas);
        },
        error: function (e, nnnn) {
            console.error('数据获取失败');
            $('.warning-content span').html('数据获取失败');
            $('.warning').css('display', 'block');
            return;
        }
    })
}

function DownloadFile(url) {
    try{
        var elemIF = document.createElement("iframe");
        elemIF.src = url;
        elemIF.style.display = "none";
        document.body.appendChild(elemIF);
        console.log("Download file Done!");
    }catch(e){
        console.log("Download file Error!");
    }
}
